<template>
  <div class="seecomic">
    <div class="switchBack" :style="{ height }">
      <span class="onback" @click="back">&lt;返回</span>
      <div class="title">
        <h3>{{ title }}</h3>
        <span>第{{ index }}话</span>
      </div>
    </div>
    <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
      <van-cell>
        <template #title>
          <ul ref="ul">
            <li v-for="(item, index) in comicsImgData" :key="index">
              <img :src="item.url + '?token=' + item.token" alt />
            </li>
          </ul>
        </template>
      </van-cell>
    </van-list>
    <div class="switchChapters" :style="{ height }">
      <span class="onup" @click="uptoComic">上一话</span>
      <span class="ondown" @click="downtoComic">下一话</span>
    </div>
    <div class="showHidden" @click="showHidden"></div>
  </div>
</template>

<script>
import { getcomicImg, getcomicToken } from "@/api/api";
import { Toast } from "vant";
export default {
  name: "Seecomic",
  data() {
    return {
      comicsImgData: [],
      loading: false,
      finished: false,
      title: "",
      index: 0,
      height: "50px",
      isHidden: false,
      allList: "",
      //漫画章节id
      id: "",
      //漫画id
      comicId: ""
    };
  },
  created() {
    this.allList = JSON.parse(localStorage.getItem("allList"));
    this.comicId = this.$route.query.comicId;
    this.comicChapterId = this.$route.query.comicChapterId;
    this.title = this.$route.query.title;
    this.index = this.$route.query.index;
    //获取漫画章节内容图片&索引
    this.getComicId(this.comicChapterId);
    //获取本地保存对应漫画的章节
    this.continuetoSeeComic(this.comicId, this.comicChapterId, this.index);
  },
  methods: {
    back() {
      let path = this.$route.query.path;
      this.$router.push(path);
    },
    // 异步更新数据
    // setTimeout 仅做示例，真实场景中一般为 ajax 请求
    onLoad() {
      setTimeout(() => {
        for (let i = 0; i < this.comicsImgData.length; i++) {
          // 数据全部加载完成
          if (this.comicsImgData.length) {
            this.finished = true;
          }
        }
        // 加载状态结束
        this.loading = false;
      }, 1000);
    },
    //上一话
    uptoComic() {
      if (this.index == 1) {
        Toast("已经是第一话了!");
        return;
      }
      this.index--;
      this.id = this.allList[this.index].id;
      //获取本地保存对应漫画的章节
      this.getComicId(this.id);
      this.continuetoSeeComic(this.comicId, this.id, this.index);
      //跳回顶层
      this.$nextTick(() => {
        this.$refs.ul.scrollTop = 0;
      });
    },
    //下一话
    downtoComic() {
      if (this.index > this.allList.length - 1) {
        Toast("已经是最后一话了!");
        return;
      }
      this.index++;
      this.id = this.allList[this.index - 1].id;
      //获取本地保存对应漫画的章节
      this.getComicId(this.id);
      this.continuetoSeeComic(this.comicId, this.id, this.index);
      //跳回顶层
      this.$nextTick(() => {
        this.$refs.ul.scrollTop = 0;
      });
    },
    //本地记录漫画观看章节,保持续看
    continuetoSeeComic(comicId, chapter, index) {
      let continueTheChapter = localStorage.getItem("continueTheChapter");
      if (continueTheChapter) {
        continueTheChapter = JSON.parse(continueTheChapter);
      } else {
        continueTheChapter = [];
      }
      //判断是否为重复漫画,如是不添加,只改变该漫画章节
      for (let i = 0; i < continueTheChapter.length; i++) {
        if (continueTheChapter[i].comicId === comicId) {
          continueTheChapter[i].chapter = chapter;
          continueTheChapter[i].index = index;
          localStorage.setItem(
            "continueTheChapter",
            JSON.stringify(continueTheChapter)
          );
          return;
        }
      }
      continueTheChapter.push({
        comicId,
        chapter,
        index
      });
      localStorage.setItem(
        "continueTheChapter",
        JSON.stringify(continueTheChapter)
      );
    },

    //获取漫画章节内容图片&索引
    getComicId(id) {
      getcomicImg(id)
        .then(res => {
          console.log(id)
          res.data.data.images = res.data.data.images.map(item => {
            item = "https://manga.hdslb.com" + item.path + "@660w.jpg";
            return item;
          });
          let str = "[";
          res.data.data.images.map((item, index) => {
            if (index === res.data.data.images.length - 1) {
              str += `"${item}"]`;
            } else {
              str += `"${item}",`;
            }
          });
          console.log(str)
          //获取对应漫画章节内容图片Token
          getcomicToken(str)
            .then(res => {
              this.comicsImgData = res.data.data;
              console.log( res.status)
            })
            .catch(err => {
              console.log(err);
            });
        })
        .catch(err => {
          console.log(err);
        });
    },
    //显示/隐藏上下指示框
    showHidden() {
      if (this.isHidden) {
        this.height = "50px";
      } else {
        this.height = "0";
      }
      this.isHidden = !this.isHidden;
    }
  }
};
</script>

<style lang="scss" scoped>
.seecomic {
  position: relative;

  .switchBack {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    background: #000;
    transition: height 0.5s;
    display: flex;
    align-items: center;
    color: #fff;
    padding: 0 10px;
    z-index: 999;
    overflow: hidden;

    .onback {
      font-size: 16px;
    }

    .title {
      display: flex;
      align-items: center;
      flex: 1;
      justify-content: center;

      h3 {
        margin: 0;
        width: 100px;
        font-size: 18px;
        margin-right: 10px;
        white-space: nowrap;
        text-overflow: ellipsis;
        overflow: hidden;
        text-align: center;
      }

      span {
        font-size: 14px;
      }
    }
  }

  &:hover ::v-deep .van-nav-bar__content {
    height: 46px;
  }

  ::v-deep .van-cell {
    height: 100vh;
    padding: 0;

    ul {
      height: 100vh;
      overflow-y: auto;

      li {
        img {
          width: 100%;
          vertical-align: bottom;
        }
      }
    }
  }

  ::v-deep .van-nav-bar__text,
  ::v-deep .van-nav-bar .van-icon {
    color: #fff;
    font-size: 16px;
  }

  .switchChapters {
    position: fixed;
    bottom: 0;
    left: 0;
    background: #000;
    width: 100%;
    text-align: center;
    line-height: 50px;
    transition: height 0.5s;

    span {
      color: #fff;
      display: inline-block;
      width: 60px;
      height: 30px;
      text-align: center;
      line-height: 30px;
      border: 1px solid #fff;
      margin-right: 10px;
      border-radius: 10px;
      font-size: 14px;
    }
  }

  .showHidden {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 140px;
    height: 140px;
    transform: translate(-50%, -50%);
  }
}
</style>